package servlet;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Addition;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import persistence.AdditionDao;
import persistence.CustomerDao;

/**
 * Servlet implementation class CreateQA
 */
@WebServlet("/CreateQA")
public class CreateQA extends MyHttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	//TODO Connect the Database
	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		Document xmlDoc = getDocument(request);
		NodeList posContent = xmlDoc.getElementsByTagName("content").item(0).getChildNodes();
		String username = posContent.item(0).getFirstChild().getNodeValue();
		long id = Long.parseLong(posContent.item(1).getFirstChild().getNodeValue());
		String body = posContent.item(2).getFirstChild().getNodeValue();
		
		Addition addition = new Addition();
		addition.setDate(new Timestamp(System.currentTimeMillis()));
		addition.setDescription(body);
		AdditionDao.save(CustomerDao.nameToID(username), id, addition);
		
	}

}
